home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / src / pl-index.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-07  |  14.3 KB  |  631 lines

  1. /*  $Id: pl-index.c,v 1.21 1997/08/07 07:58:08 jan Exp $
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     See ../LICENCE to find out about your rights.
  5.     jan@swi.psy.uva.nl
  6.  
  7.     Purpose: indexing support
  8. */
  9.  
  10. #include "pl-incl.h"
  11.  
  12. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  13. Clause indexing.  Clauses store an  `index  structure',  which  provides
  14. summary information on the unification behaviour of the clause (e.i. its
  15. head  arguments.   This  structure  consists  of  two words: a key and a
  16. varmask.  Indexing can be done with upto 4 arguments.   Both  words  are
  17. divided  into  the  same  number  of  bit  groups  as  there are indexed
  18. arguments.  If an argument  is  indexable  (atom,  integer  or  compound
  19. term),  the  corresponding  bit group is filled with bits taken from the
  20. atom  pointer,  integer  or  functor  pointer.    In   this   case   all
  21. corresponding  bits  in  the varmask field are 1.  Otherwise the bits in
  22. both the varmask and the key are all 0.
  23.  
  24. To find a clause using indexing, we calculate an  index  structure  from
  25. the  calling arguments to the goal using the same rules.  Now, we can do
  26. a mutual `and' using the varmasks on the keys and  compare  the  result.
  27. If  equal  a  good  chance  for a possible unification exists, otherwise
  28. unification will definitely fail.  See matchIndex() and findClause().
  29.  
  30. Care has been taken to get this code as fast as  possible,  notably  for
  31. indexing only on the first argument as this is default.
  32. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  33.  
  34. /* 1 <= c <= 4 */
  35.  
  36. #define SHIFT(c, a)    ((LONGBITSIZE/(c)) * a)
  37. #define MASK(c)        (c == 1 ? ~0L : ((1L << (LONGBITSIZE/(c))) - 1))
  38. #define VM(c, a)    ((unsigned long)(~(MASK(c) << SHIFT(c, a))))
  39.  
  40. #define Shift(c, a)    (mask_shift[c][a])
  41. #define Mask(c)        (mask_mask[c])
  42. #define varMask(c, a)    (variable_mask[c][a])
  43.  
  44. #define matchIndex(i1, i2)    (((i1).key & (i2).varmask) ==\
  45.                   ((i2).key & (i1).varmask))
  46.  
  47. static unsigned long variable_mask[][4] =
  48.   { { 0,        0,        0,        0 }, 
  49. #ifdef DONOT_AVOID_SHIFT_WARNING
  50.     { VM(1, 0), 0,        0,        0 },
  51. #else
  52.     { (unsigned long)~0L,      0,        0,        0 },
  53. #endif
  54.     { VM(2, 0), VM(2, 1), 0,        0 }, 
  55.     { VM(3, 0), VM(3, 1), VM(3, 2), 0 }, 
  56.     { VM(4, 0), VM(4, 1), VM(4, 2), VM(4, 3) }
  57.   };
  58.  
  59. static int mask_shift[][4] =
  60.   { { 0,           0,           0,           0 }, 
  61.     { SHIFT(1, 0), 0,           0,           0 }, 
  62.     { SHIFT(2, 0), SHIFT(2, 1), 0,           0 }, 
  63.     { SHIFT(3, 0), SHIFT(3, 1), SHIFT(3, 2), 0 }, 
  64.     { SHIFT(4, 0), SHIFT(4, 1), SHIFT(4, 2), SHIFT(4, 3) }
  65.   };
  66.  
  67. static unsigned long mask_mask[] =
  68.   { 0,
  69. #ifdef DONOT_AVOID_SHIFT_WARNING
  70.     MASK(1),
  71. #else
  72.     0L,
  73. #endif
  74.     MASK(2), MASK(3), MASK(4)
  75.   };
  76.  
  77.  
  78. int
  79. cardinalityPattern(register unsigned long pattern)
  80. { register int result = 0;
  81.  
  82.   for(; pattern; pattern >>= 1)
  83.     if (pattern & 0x1)
  84.       result++;
  85.  
  86.   return result;
  87. }
  88.  
  89.  
  90. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  91. Compute the index in the hash-array from   a machine word and the number
  92. of buckets. This used to be simple, but now that our tag bits are on the
  93. left side, simply masking will put most things on the same hash-entry as
  94. it is very common for all clauses of   a predicate to have the same type
  95. of object. Hence, we now use exclusive or of the real value part and the
  96. tag-bits.
  97. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  98.  
  99. static inline int
  100. hashIndex(word key, int buckets)
  101. { unsigned long k = key >> LMASK_BITS;
  102.  
  103.   return (key^k) & (buckets-1);
  104. }
  105.  
  106.  
  107. static word
  108. indexOfWord(word w)
  109. { for(;;)
  110.   { switch(tag(w))
  111.     { case TAG_VAR:
  112.       case TAG_STRING:
  113.       case TAG_FLOAT:
  114.     return 0L;
  115.       case TAG_INTEGER:
  116.     if ( storage(w) != STG_INLINE )
  117.       return 0L;
  118.       case TAG_ATOM:
  119.     return w;
  120.       case TAG_COMPOUND:
  121.     return *valPtr(w);
  122.       case TAG_REFERENCE:
  123.     w = *unRef(w);
  124.     continue;
  125.     }
  126.   }
  127. }
  128.  
  129.  
  130. void
  131. getIndex(register Word argv, register unsigned long pattern, int card,
  132.      struct index *index)
  133. { if ( pattern == 0x1L )
  134.   { index->key     = indexOfWord(*argv);
  135.     index->varmask = (index->key ? (unsigned long) ~0L : 0L);
  136.  
  137.     return;
  138.   } else
  139.   { word key;
  140.     int a;
  141.  
  142.     index->key = 0;
  143.     index->varmask = (unsigned long) ~0L;            /* all 1s */
  144.  
  145.     for(a = 0; a < card; a++, pattern >>= 1, argv++)
  146.     { for(;(pattern & 0x1) == 0; pattern >>= 1)
  147.     argv++;
  148.  
  149.       key = indexOfWord(*argv);
  150.       if ( !key )
  151.       { index->varmask &= varMask(card, a);
  152.       }
  153.       key = key ^ (key >> LMASK_BITS);    /* see hashIndex() */
  154.       index->key |= ((key & Mask(card)) << Shift(card, a) );
  155.     }
  156.   }
  157.  
  158.   return;
  159. }
  160.  
  161.  
  162. ClauseRef
  163. findClause(ClauseRef cref, Word argv, Definition def, bool *deterministic)
  164. { if ( def->indexPattern == 0x0L )
  165.   { noindex:
  166.     for(;;cref = cref->next)
  167.     { if ( cref )
  168.       { if ( false(cref->clause, ERASED) )
  169.     { *deterministic = !cref->next;
  170.       return cref;
  171.     }
  172.       } else
  173.     return NULL;
  174.     }
  175.   } else if ( def->indexPattern == 0x1L )
  176.   { word key = indexOfWord(*argv);
  177.  
  178.     if ( !key )
  179.       goto noindex;
  180.  
  181.     for(;cref ; cref = cref->next)
  182.     { Clause clause = cref->clause;
  183.  
  184.       if ( (key & clause->index.varmask) == clause->index.key &&
  185.        false(clause, ERASED))
  186.       { ClauseRef result = cref;
  187.       
  188.     for( cref = cref->next; cref; cref = cref->next )
  189.     { clause = cref->clause;
  190.       if ( (key&clause->index.varmask) == clause->index.key &&
  191.            false(clause, ERASED))
  192.       { *deterministic = FALSE;
  193.  
  194.         return result;
  195.       }
  196.     }
  197.     *deterministic = TRUE;
  198.  
  199.     return result;
  200.       }
  201.     }
  202.     return NULL;
  203.   } else if ( def->indexPattern & NEED_REINDEX )
  204.   { reindexDefinition(def);
  205.     return findClause(cref, argv, def, deterministic);
  206.   } else
  207.   { struct index argIndex;
  208.  
  209.     getIndex(argv, def->indexPattern, def->indexCardinality, &argIndex);
  210.     for(; cref; cref = cref->next)
  211.     { if ( matchIndex(argIndex, cref->clause->index) &&
  212.        false(cref->clause, ERASED))
  213.       { ClauseRef result = cref;
  214.       
  215.     for( cref = cref->next; cref; cref = cref->next )
  216.     { if ( matchIndex(argIndex, cref->clause->index) &&
  217.            false(cref->clause, ERASED))
  218.       { *deterministic = FALSE;
  219.  
  220.         return result;
  221.       }
  222.     }
  223.     *deterministic = TRUE;
  224.  
  225.     return result;
  226.       }
  227.     }
  228.     return NULL;
  229.   }
  230. }
  231.  
  232.  
  233. static ClauseRef
  234. nextClause(ClauseRef cref, bool *det, Index ctx)
  235. { if ( ctx->varmask == ~0x0L )        /* first argument only */
  236.   { word key = ctx->key;
  237.  
  238.     for(;cref ; cref = cref->next)
  239.     { Clause clause = cref->clause;
  240.  
  241.       if ( (key & clause->index.varmask) == clause->index.key &&
  242.        false(clause, ERASED))
  243.       { ClauseRef result = cref;
  244.       
  245.     for( cref = cref->next; cref; cref = cref->next )
  246.     { clause = cref->clause;
  247.       if ( (key&clause->index.varmask) == clause->index.key &&
  248.            false(clause, ERASED))
  249.       { *det = FALSE;
  250.  
  251.         return result;
  252.       }
  253.     }
  254.     *det = TRUE;
  255.  
  256.     return result;
  257.       }
  258.     }
  259.   } else if ( ctx->varmask == 0x0L )    /* no indexing */
  260.   { for(; cref; cref = cref->next)
  261.     { if ( false(cref->clause, ERASED) )
  262.       { *det = !cref->next;
  263.         return cref;
  264.       }
  265.     }
  266.   } else                /* general (multi-arg) indexing */
  267.   { for(; cref; cref = cref->next)
  268.     { if ( matchIndex(*ctx, cref->clause->index) &&
  269.        false(cref->clause, ERASED))
  270.       { ClauseRef result = cref;
  271.       
  272.     for( cref = cref->next; cref; cref = cref->next )
  273.     { if ( matchIndex(*ctx, cref->clause->index) &&
  274.            false(cref->clause, ERASED))
  275.       { *det = FALSE;
  276.  
  277.         return result;
  278.       }
  279.     }
  280.     *det = TRUE;
  281.  
  282.     return result;
  283.       }
  284.     }
  285.   }
  286.  
  287.   return NULL;
  288. }
  289.  
  290.  
  291. static ClauseRef
  292. firstClause(Word argv, Definition def, bool *det)
  293. { ClauseRef cref;
  294.   struct index buf;
  295.   Index ctx = &buf;
  296.  
  297. again:
  298.   if ( def->indexPattern == 0x0L )
  299.   {
  300.   noindex:
  301.     for(cref = def->definition.clauses; cref; cref = cref->next)
  302.     { if ( false(cref->clause, ERASED) )
  303.       { *det = !cref->next;
  304.         return cref;
  305.       }
  306.     }
  307.     return NULL;
  308.   } else if ( def->indexPattern == 0x1L )
  309.   { word key = indexOfWord(*argv);
  310.  
  311.     if ( key == 0L )
  312.       goto noindex;
  313.  
  314.     ctx->key     = key;
  315.     ctx->varmask = (unsigned long) ~0x0L;
  316.     if ( def->hash_info )
  317.     { int hi = hashIndex(key, def->hash_info->buckets);
  318.  
  319.       cref = def->hash_info->entries[hi].head;
  320.     } else
  321.       cref = def->definition.clauses;
  322.   } else if ( def->indexPattern & NEED_REINDEX )
  323.   { reindexDefinition(def);
  324.     goto again;
  325.   } else
  326.   { getIndex(argv, def->indexPattern, def->indexCardinality, ctx);
  327.     cref = def->definition.clauses;
  328.   }
  329.  
  330.   return nextClause(cref, det, ctx);
  331. }
  332.  
  333.  
  334. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  335. Recalculate the index of  a  clause  after  the  index  pattern  on  the
  336. predicate  has been changed.  The head of the clause is decompiled.  The
  337. resulting term is simply discarded as it cannot have links to any  other
  338. part of the stacks (e.g. backtrailing is not needed).
  339. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  340.  
  341. bool
  342. reindexClause(Clause clause)
  343. { Procedure proc = clause->procedure;
  344.   unsigned long pattern = proc->definition->indexPattern;
  345.  
  346.   if ( pattern == 0x0 )
  347.     succeed;
  348.   if ( false(clause, ERASED) )
  349.   { fid_t fid = PL_open_foreign_frame();
  350.     
  351.     if ( pattern == 0x1 )        /* the 99.9% case.  Speedup a little */
  352.     { word key;
  353.  
  354.       if ( arg1Key(clause, &key) )
  355.       { clause->index.key     = key;
  356.     clause->index.varmask = (unsigned long)~0L;
  357.       } else
  358.       { clause->index.key     = 0L;
  359.     clause->index.varmask = 0L;
  360.       }
  361.     } else
  362.     { term_t head = PL_new_term_ref();
  363.  
  364.       decompileHead(clause, head);
  365.       getIndex(argTermP(*valTermRef(head), 0),
  366.            pattern,
  367.            proc->definition->indexCardinality,
  368.            &clause->index);
  369.     }
  370.  
  371.     PL_discard_foreign_frame(fid);
  372.   }
  373.  
  374.   succeed;
  375. }
  376.  
  377.  
  378. bool
  379. unify_index_pattern(Procedure proc, term_t value)
  380. { Definition def = proc->definition;
  381.   unsigned long pattern = (def->indexPattern & ~NEED_REINDEX);
  382.   int n, arity = def->functor->arity;
  383.  
  384.   if ( pattern == 0 )
  385.     fail;
  386.  
  387.   if ( PL_unify_functor(value, def->functor->functor) )
  388.   { term_t a = PL_new_term_ref();
  389.  
  390.     for(n=0; n<arity; n++, pattern >>= 1)
  391.     { if ( !PL_get_arg(n+1, value, a) ||
  392.        !PL_unify_integer(a, (pattern & 0x1) ? 1 : 0) )
  393.     fail;
  394.     }
  395.  
  396.     succeed;
  397.   }
  398.  
  399.   fail;
  400. }
  401.  
  402.          /*******************************
  403.          *       HASH SUPPORT        *
  404.          *******************************/
  405.  
  406. static ClauseIndex
  407. newClauseIndexTable(int buckets)
  408. { ClauseIndex ci = allocHeap(sizeof(struct clause_index));
  409.   ClauseChain ch;
  410.   int m = 4;
  411.  
  412.   while(m<buckets)
  413.     m *= 2;
  414.   buckets = m;
  415.  
  416.   ci->buckets  = buckets;
  417.   ci->size     = 0;
  418.   ci->alldirty = FALSE;
  419.   ci->entries  = allocHeap(sizeof(struct clause_chain) * buckets);
  420.   
  421.   for(ch = ci->entries; buckets; buckets--, ch++)
  422.   { ch->head = ch->tail = NULL;
  423.     ch->dirty = 0;
  424.   }
  425.  
  426.   return ci;
  427. }
  428.  
  429.  
  430. void
  431. unallocClauseIndexTable(ClauseIndex ci)
  432. { ClauseChain ch;
  433.   int buckets = ci->buckets;
  434.  
  435.   for(ch = ci->entries; buckets; buckets--, ch++)
  436.   { ClauseRef cr, next;
  437.  
  438.     for(cr = ch->head; cr; cr = next)
  439.     { next = cr->next;
  440.       freeHeap(cr, sizeof(*cr));
  441.     }
  442.   }
  443.   
  444.   freeHeap(ci->entries, ci->buckets * sizeof(struct clause_chain));
  445.   freeHeap(ci, sizeof(struct clause_index));
  446. }
  447.  
  448.  
  449. static void
  450. appendClauseChain(ClauseChain ch, Clause cl, int where)
  451. { ClauseRef cr = newClauseRef(cl);
  452.  
  453.   if ( !ch->tail )
  454.     ch->head = ch->tail = cr;
  455.   else
  456.   { if ( where != CL_START )
  457.     { ch->tail->next = cr;
  458.       ch->tail = cr;
  459.     } else
  460.     { cr->next = ch->head;
  461.       ch->head = cr;
  462.     }
  463.   }
  464. }
  465.  
  466.  
  467. static void
  468. deleteClauseChain(ClauseChain ch, Clause clause)
  469. { ClauseRef prev = NULL;
  470.   ClauseRef c;
  471.  
  472.   for(c = ch->head; c; prev = c, c = c->next)
  473.   { if ( c->clause == clause )
  474.     { if ( !prev )
  475.       { ch->head = c->next;
  476.     if ( !c->next )
  477.       ch->tail = NULL;
  478.       } else
  479.       { prev->next = c->next;
  480.     if ( !c->next)
  481.       ch->tail = prev;
  482.       }
  483.     }
  484.   }
  485. }
  486.  
  487.  
  488. static int
  489. gcClauseChain(ClauseChain ch, int dirty)
  490. { ClauseRef cref = ch->head, prev = NULL;
  491.   int deleted = 0;
  492.  
  493.   while( cref && dirty )
  494.   { if ( true(cref->clause, ERASED) )
  495.     { ClauseRef c = cref;
  496.       
  497.       if ( c->clause->index.varmask != 0 ) /* indexed and only in this */
  498.     deleted++;               /* chain */
  499.       dirty--;
  500.  
  501.       cref = cref->next;
  502.       if ( !prev )
  503.       { ch->head = c->next;
  504.     if ( !c->next )
  505.       ch->tail = NULL;
  506.       } else
  507.       { prev->next = c->next;
  508.     if ( c->next == NULL)
  509.       ch->tail = prev;
  510.       }
  511.  
  512.       freeClauseRef(c);
  513.     } else
  514.     { prev = cref;
  515.       cref = cref->next;
  516.     }
  517.   }
  518.  
  519.   ch->dirty = 0;
  520.  
  521.   return deleted;
  522. }
  523.  
  524.  
  525. #define INFINT (~(1<<(INTBITSIZE-1)))
  526.  
  527. void
  528. gcClauseIndex(ClauseIndex ci)
  529. { ClauseChain ch = ci->entries;
  530.   int n = ci->buckets;
  531.     
  532.   if ( ci->alldirty )
  533.   { for(; n; n--, ch++)
  534.       ci->size -= gcClauseChain(ch, INFINT);
  535.   } else
  536.   { for(; n; n--, ch++)
  537.     { if ( ch->dirty )
  538.     ci->size -= gcClauseChain(ch, ch->dirty);
  539.     }
  540.   }
  541. }
  542.  
  543.  
  544. void
  545. markDirtyClauseIndex(ClauseIndex ci, Clause cl)
  546. { if ( cl->index.varmask == 0 )
  547.     ci->alldirty = TRUE;
  548.   else
  549.   { int hi = hashIndex(cl->index.key, ci->buckets);
  550.     ci->entries[hi].dirty++;
  551.   }
  552. }
  553.  
  554.  
  555. void
  556. addClauseToIndex(Definition def, Clause cl, int where)
  557. { ClauseIndex ci = def->hash_info;
  558.   ClauseChain ch = ci->entries;
  559.  
  560.   if ( cl->index.varmask == 0 )        /* a non-indexable field */
  561.   { int n = ci->buckets;
  562.     
  563.     for(; n; n--, ch++)
  564.       appendClauseChain(ch, cl, where);
  565.   } else
  566.   { int hi = hashIndex(cl->index.key, ci->buckets);
  567.     
  568.     DEBUG(2, Sdprintf("Storing in bucket %d\n", hi));
  569.     appendClauseChain(&ch[hi], cl, where);
  570.  
  571.     if ( ++ci->size / 2 > ci->buckets )
  572.     { enterDefinition(def);
  573.       set(def, NEEDSREHASH);
  574.       leaveDefinition(def);
  575.     }
  576.   }
  577. }
  578.  
  579.  
  580. void
  581. delClauseFromIndex(ClauseIndex ci, Clause cl)
  582. { ClauseChain ch = ci->entries;
  583.  
  584.   if ( cl->index.varmask == 0 )        /* a non-indexable field */
  585.   { int n = ci->buckets;
  586.     
  587.     for(; n; n--, ch++)
  588.       deleteClauseChain(ch, cl);
  589.   } else
  590.   { int hi = hashIndex(cl->index.key, ci->buckets);
  591.     
  592.     deleteClauseChain(&ch[hi], cl);
  593.     ci->size--;
  594.   }
  595. }
  596.  
  597.  
  598. bool
  599. hashDefinition(Definition def, int buckets)
  600. { ClauseRef cref;
  601.  
  602.   if ( true(def, FOREIGN) )
  603.     fail;
  604.   if ( def->indexPattern != 0x1 )
  605.     fail;
  606.  
  607.   def->hash_info = newClauseIndexTable(buckets);
  608.  
  609.   for(cref = def->definition.clauses; cref; cref = cref->next)
  610.     addClauseToIndex(def, cref->clause, CL_END);
  611.  
  612.   succeed;
  613.  
  614. }
  615.  
  616. word
  617. pl_hash(term_t pred)
  618. { Procedure proc;
  619.  
  620.   if ( get_procedure(pred, &proc, 0, GP_CREATE) )
  621.   { Definition def = proc->definition;
  622.  
  623.     if ( false(def, FOREIGN) && def->indexPattern & NEED_REINDEX )
  624.       reindexDefinition(def);
  625.  
  626.     return hashDefinition(def, 256);
  627.   }
  628.  
  629.   fail;
  630. }
  631.